package gzhaoku.com.common.util;

import android.os.Environment;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import gzhaoku.com.common.Logger;

/**
 * 文件工具
 */
public class FileUtil {

    static final Logger log = Logger.getLog(FileUtil.class);

    private String SDPATH;

    public FileUtil() {
        //得到外部存储设备的目录（/SDCARD）
        SDPATH = Environment.getExternalStorageDirectory() + "/" ;
    }

    public String getSDPATH() {
        return SDPATH;
    }

    public FileUtil(String SDPATH){
        this.SDPATH = SDPATH;
    }

    /**
     * 在SD卡上创建文件
     * @param fileName
     * @return
     * @throws java.io.IOException
     */
    public File createSDFile(String fileName) throws IOException {
        File file = new File(SDPATH + fileName);
//        log.i("createSDFile:"+file.getAbsolutePath());
//        file.createNewFile();
        return file;
    }

    /**
     * 在SD卡上创建目录
     * @param dirName 目录名字
     * @return 文件目录
     */
    public File createDir(String dirName){
        File dir = new File(SDPATH + dirName);
        dir.mkdirs();
        return dir;
    }

    /**
     * 判断文件是否存在
     * @param fileName
     * @return
     */
    public boolean isFileExist(String fileName){
        File file = new File(SDPATH + fileName);
        return file.exists();
    }

    /**
     * 获取文件
     * @param fileName
     * @return
     */
    public File getFile(String fileName){
        File file = new File(SDPATH + fileName);
        return file;
    }

    public File write2SDFromInput(String path, String fileName, InputStream input){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        File file = null;
        OutputStream output = null;

        try{
            createDir(path);
            file =createSDFile(path + "/"+ fileName);
            output = new FileOutputStream(file);

            bis = new BufferedInputStream(input);
            bos = new BufferedOutputStream(output);

            byte[] b = new byte[1024];
            int len;
            while((len = bis.read(b)) != -1){
                bos.write(b, 0, len);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            IOUtils.close(bos, output, bis, input);
        }
//        File file = null;
//        OutputStream output = null;
//
//        try {
//            createDir(path);
//            file =createSDFile(path + fileName);
//            output = new FileOutputStream(file);
//            byte [] buffer = new byte[512 * 1024];
//            while(input.read(buffer) != -1){
//                output.write(buffer);
//                output.flush();
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        finally {
//            try {
//                output.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
        return file;
    }

    public void deleteFile(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                File f = files[i];
                deleteFile(f);
            }
            file.delete();//如要保留文件夹，只删除文件，请注释这行
        } else if (file.exists()) {
            file.delete();
        }
    }
}
